package org.shijiagen.user.controller;

import org.shijiagen.param.AddressListParam;
import org.shijiagen.param.AddressParam;
import org.shijiagen.param.AddressRemoveParam;
import org.shijiagen.pojo.Address;
import org.shijiagen.user.service.AddressService;
import org.shijiagen.utils.R;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("user/address")
@RequiredArgsConstructor
public class AddressController {

    private final AddressService addressService;

    @PostMapping("list")
    public R list(@RequestBody @Validated AddressListParam addressListParam){
        return R.ok("查询成功",addressService.list(addressListParam.getUserId()));
    }

    @PostMapping("save")
    public R save(@RequestBody @Validated AddressParam addressParam){
        Address address = addressParam.getAdd();
        address.setUserId(addressParam.getUserId());
        List<Address> addresses = addressService.save(address);
        return addresses == null ? R.fail("参数异常，保存失败！"):R.ok("保存成功!",addresses);
    }

    @PostMapping("remove")
    public R remove(@RequestBody @Validated AddressRemoveParam addressRemoveParam){
        return addressService.remove(addressRemoveParam)?R.ok("删除成功！"):R.fail("删除失败！");
    }
}
